Preliminaries

The MEAN stack is a free and open-source framework for creating web applications. The acronym stands for MongoDB, Express.js, Angular.js, and node.js — these are the core components of the MEAN stack. It is very popular with web developers because it uses JavaScript throughout the entire stack, allowing for uniformity between application components. In this tutorial we’ll show you how to install all the components correctly and get started with MEAN.

We’ll assume that you’ve followed our “Getting Started With Your Stack” tutorial and have created a non-root user with sudo privileges.

Installing the Components

We’ll begin by installing MongoDB.

Installing MongoDB

First, we’ll need to add the public key used by aptitude package repositories to ensure that we’re downloading a valid package using the package manager.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

You might be prompted whether you actually want to add the key to the list of authorized keys. You should press enter to affirm the prompt. Then we need to actually add the URL of the repositories to our local list of available repositories.

echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

Then we’ll need to update the repositories and ensure that we’re getting the most recent stable version.

sudo apt-get update

Finally, we can install the MongoDB package

sudo apt-get install -y mongodb-org

Finally, if you’re running a Stack that has less than 10GB of SSD, you will have trouble with MongoDB unless you enable the ‘smallfiles’ configuration flag. You can do this by adding ‘smallfiles = true’ to the ‘mongod.conf’ in the ‘/etc’ folder. Run the following commands:

echo "smallfiles = true" >> /etc/mongod.conf
service mongod restart

Installing node.js

In the same way as we did with MongoDB, we need to add a third-party repository to our local list of available repositories in order to download node.js

sudo add-apt-repository ppa:chris-lea/node.js

Now update the package repositories once again to get the latest version of node.js and then install the package itself, along with the node package manager (npm)

sudo apt-get update
sudo apt-get install nodejs npm

Installing npm Packages

The MEAN stack requires the use of two additional modules that it uses to manage front-end packages (Bower) and automate tasks needed to build and serve your application (Grunt). Since npm we already installed npm, installing these two packages is as simple as running the following commands.

sudo npm install -g bower
sudo npm install -g grunt-cli

Pulling the MEAN.JS project

Now that we have all the necessary components installed, we can clone the MEAN.JS project from the public git repository, install project dependencies, and begin developing. However, we do need to install git on our system. Fortunately that only takes one command:

sudo apt-get install git

Once git is installed, change directories into the folder in which you wish to house your MEAN projects. For example, I like storing my projects in the ‘/opt’ folder of the machine.

cd /opt

Now we can pull the codebase from the git repo and specify a folder in which to clone the files. Run the following command, replacing the ‘[your project name]’ placeholder appropriately.

git clone https://github.com/meanjs/mean.git [your project name]

This will create a folder in your working directory that has the name of the project name you specified while cloning the MEAN files. Change directories into that folder and run sudo npm install to install all the dependencies required for MEAN.JS. This will likely take a few minutes, but once it’s done you’re all set to start developing your MEAN application. Run grunt to start the webserver on port 3000. You can test that everything worked correctly by pointing your browser to http://your_ip_address:3000.

Final Words

Congratulations! You have everything required to start developing blazing fast web applications using the MEAN framework. For more information about the structure of a MEAN application, about how to begin developing using MEAN, and about anything else regarding the framework, check out the documentation. From all of us at Stack Harbor, ahoy!